package com.group3.merchant.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.group3.entity.Merchant;
import com.group3.merchant.service.IMerchantService;
import com.group3.merchant.service.impl.MerchantServiceImpl;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IMerchantService service = new MerchantServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean isSuccess=register(request, response);
		if(isSuccess)
		{
			response.sendRedirect("login.jsp");
		}else {
			System.out.println("register fail");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean isSuccess=register(request, response);
		if(isSuccess)
		{
			response.sendRedirect("login.jsp");
		}else {
			System.out.println("register fail");
		}
	}
	private boolean register(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		boolean isSuccess=false;
		boolean isMultipart=FileUpload.isMultipartContent(request);
		if(isMultipart)
		{
			DiskFileItemFactory factory=new DiskFileItemFactory();
			ServletFileUpload upload=new ServletFileUpload(factory);
			String filePath="";
			Merchant merchant=new Merchant();
			try {
				List<FileItem> list=upload.parseRequest(request);
				Iterator<FileItem> i=list.iterator();
				while(i.hasNext())
				{
					FileItem item=i.next();
					if (item.isFormField()) {
						switch (item.getFieldName()) {
						case "mName":
							merchant.setmName(item.getString("utf-8"));
							break;
						case "password":
							merchant.setPassword(item.getString("utf-8"));
							break;
						case "address":
							merchant.setAddress(item.getString("utf-8"));
							break;
						default:
							break;
						}
					}else {
						switch (item.getFieldName()) {
						case "image":
							File imageFile=new File("d://AMC/Header/"+UUID.randomUUID()+item.getName());
							filePath=imageFile.getAbsolutePath();
							merchant.setImageUrl(filePath);
							item.write(imageFile);
							break;
						case "idCard":
							File idCardFile=new File("d://AMC/IDCard/"+UUID.randomUUID()+item.getName());
							filePath=idCardFile.getAbsolutePath();
							merchant.setIdCardUrl(filePath);
							item.write(idCardFile);
							break;
						default:
							break;
						}
					}
				}
				//调service
				System.out.println(merchant);
				Merchant merchant2=service.register(merchant);
				if(merchant2!=null) 
					isSuccess=true;
				System.out.println(merchant);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return isSuccess;
		
	}

}
